// pages/kanjian/kanjian.js
var util=require("../../utils/util.js");
var timer=require("../../utils/wxTimer.js");
var app=getApp();
var baseImgUrl = util.baseImgUrl;
var goodsPic1 = baseImgUrl + "erji.png";
var goodsPic2 = baseImgUrl + "huodong.png";
var isnonePic = "/pages/images/miaosha_none.png";
var mywords=["继续砍价","去领取","再砍一个"];
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ceshi:111,
    mywords:["继续砍价", "去领取","已领取", "重砍一个"],
    isnone1: false,//砍价商品是否为空，true--空 false--非空
    isnone2: false,//我的砍价是否为空，true--空 false--非空
    isnonePic: "/pages/images/miaosha_none.png",
    currentpage:1,//1--砍价商品  2--我的砍价  默认为1
    imgUrls: [baseImgUrl + "lunbo.png", baseImgUrl + "lunbo.png", baseImgUrl + "lunbo.png"],//轮播图路径
    timerPic: "/pages/images/timer.png",
    heartPic:"/pages/images/heart.png",
    goodslist:[
      {
      id: 0,
      goodspic: goodsPic2,
      goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
      num:235,
      price: 99.99,
     
    },{
        id: 1,
        goodspic: goodsPic1,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        num: '1.2w',
        price: 19,
    },{
        id: 2,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        num: 235,
        price: 99.99,
      }, {
        id: 3,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        num: 235,
        price: 99.99,
      }
      ],
    mygoodslist: [
      {
        id: 0,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        hour: "00",
        minute: "00",
        second: "12",
        price:99.99,
        status:0,//0--砍价中 1--砍价成功  2--砍价失败
      
      }, {
        id: 1,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        hour: "01",
        minute: "00",
        second:"12",
        price: 120,
        status: 1,//0--砍价中 1--砍价成功  2--砍价失败
      }, {
        id: 2,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        hour: "01",
        minute: "11",
        second: "22",
        price: 99,
        status: 2,//0--砍价中 1--砍价成功  2--砍价失败
      }, {
        id: 3,
        goodspic: goodsPic2,
        goodsname: "家用迷你电烤箱 红外线速热 控温简单 烘焙必备",
        hour: "0",
        minute: "0",
        second: "0",
        price: 120.22,
        status: 3,//0--砍价中 1--砍价成功  2--砍价失败
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log("----------------options--------------")
    console.log(options);
    countdown(that);
    if(options.key){
      if(options.key==1){
          that.setData({
            currentpage:1
          })
      }
      if(options.key == 2) {
        that.setData({
          currentpage: 2
        })
       }
    }else{
      that.setData({
        currentpage: 1
      })
    }
    

    
  },

  // 点击底部
  choseFoot:function(e){
    var that=this;
    var currentpage=e.currentTarget.dataset.currentpage;
    // var active=e.currentTarget.dataset.active;
    console.log("-----------点击-----------");
   console.log(e);
    console.log(currentpage)
    that.setData({
      currentpage: currentpage
    })
  },

  //进入砍价详情页面
  kanjiaDetail:function(){
    wx.navigateTo({
      url: '/pages/kj_detail/kj_detail',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      // this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
     
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function countdown(that) {
  var that=that;
  var len = that.data.mygoodslist.length;
   timer = setInterval(function () {
    for (var i = 0; i < len; i++) {
      // console.log("**************************循环的次数************");
      // console.log(i);
      var hours = that.data.mygoodslist[i].hour*1;
      var minutes = that.data.mygoodslist[i].minute*1;
      var seconds = that.data.mygoodslist[i].second*1;
      var status = that.data.mygoodslist[i].status;
      var times = hours * 3600 + minutes * 60 + seconds * 1;
      if (times > 0) {
        times--;
        var hour = Math.floor(times / (60 * 60));
        var minute = Math.floor(times / 60) - (hour * 60);
        var second = Math.floor(times) - (hour * 60 * 60) - (minute * 60);

        var str = [hour, minute, second]
        if (hour <= 9) hour = '0' + hour;
        if (minute <= 9) minute = '0' + minute;
        if (second <= 9) second = '0' + second;
        
        that.data.mygoodslist[i].hour = hour+"";
        that.data.mygoodslist[i].minute = minute + "";
        that.data.mygoodslist[i].second = second + "";
        // console.log(that.data.mygoodslist);
        // console.log(hour + "," + minute + "," + second)
        that.setData({
          mygoodslist: that.data.mygoodslist

        })
      
        
     } else {
       
        ///测试 假定倒计时结束后 为砍价失败
        //实际请求接口，后台返回状态值
        that.data.mygoodslist[i].status=3;
        that.setData({
          mygoodslist: that.data.mygoodslist

        })
      }
    }
   
  }, 1000)
}